﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;
using ERP_RORZE.Tool.UserControls;

namespace ERP_RORZE.Tool.Forms
{
	public partial class frm_ToolGroupEdit : DevExpress.XtraEditors.XtraForm
	{
		public string ToolGroupId;
		public string ToolGroupName;
		private ucToolGroup parent;

		public frm_ToolGroupEdit(ucToolGroup fToolGroup)
		{
			InitializeComponent();
			parent = fToolGroup;
			this.CancelButton = this.btCancel;
		}

		private void btSave_Click(object sender, EventArgs e)
		{
			ToolGroupHelper helper = new ToolGroupHelper(new ToolGroup());
			if(GetData(helper.ValueObject))
			{
				Utilities.Timer timer = new Utilities.Timer();

				if(helper.Update() > 0)
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Blue, ToolConstants.Message.UpdateSuccess);
				}
				else
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Red, ToolConstants.Message.UpdateFailed);
				}
				parent.BindDatatoGrid();
			}
		}

		private void btCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void frm_ToolGroupEdit_Load(object sender, EventArgs e)
		{
			BindData();
		}

		#region Private methods

		private bool CheckDuplicate(string id)
		{
			ToolGroupHelper helper = new ToolGroupHelper(new ToolGroup());
			helper.ValueObject.ToolGroupID = id;
			DataTable dt = helper.GetOne();
			if(dt.Rows.Count > 0)
				return true;
			return false;

		}

		private bool GetData(ToolGroup o)
		{
			#region Check valid
			if(Utilities.IsNullOrEmpty(txtTGroupID.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.ToolGroup.ToolGroupIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(txtTGroupName.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.ToolGroup.ToolGroupNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(string.Compare(txtTGroupID.Text.Trim(), ToolGroupId) != 0)
			{
				if(CheckDuplicate(txtTGroupID.Text.Trim()))
				{
					MessageBox.Show(ToolConstants.Message.ToolGroup.ToolGroupHasExisted, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
					return false;
				}
			}

			#endregion

			o.TempId = txtTGroupID.Text;
			o.ToolGroupID = ToolGroupId;
			o.ToolGroupName = txtTGroupName.Text;

			return true;
		}

		//private void BindData(ToolGroup o)
		//{
		//	txtToolGroupID.Text = o.ToolGroupID;
		//	txtToolGroupName.Text = o.ToolGroup_English;
		//	txtToolGroupJapanese.Text = o.ToolGroup_Japanese;
		//}

		private void BindData()
		{
			txtTGroupID.Text =ToolGroupId;
			txtTGroupName.Text = ToolGroupName;
		}

		#endregion

	}
}